package common.utils;

import com.alibaba.fastjson.JSON;

import java.io.InputStream;

public class JsonUtil {
    public static JSON parse(String json) throws Exception {
        return (JSON) JSON.parse(json);
    }

    public static JSON parse(InputStream stream) throws Exception {
        String json = BinaryUtil.getText(stream);
        return parse(json);
    }

    public static JSON parseFromFile(String path) throws Exception {
        String json = FileUtil.getText(path);
        return parse(json);
    }

    public static JSON parseFromResource(String path) throws Exception {
        String json = ResourceUtil.getText(path);
        return parse(json);
    }

    public static <T> T deserialize(String json, Class<T> clazz) throws Exception {
        return JSON.parseObject(json, clazz);
    }

    public static <T> T deserialize(InputStream stream, Class<T> clazz) throws Exception {
        String json = BinaryUtil.getText(stream);
        return deserialize(json, clazz);
    }

    public static <T> T deserializeFromFile(String path, Class<T> clazz) throws Exception {
        String json = FileUtil.getText(path);
        return deserialize(json, clazz);
    }

    public static <T> T deserializeFromResource(String path, Class<T> clazz) throws Exception {
        String json = ResourceUtil.getText(path);
        return deserialize(json, clazz);
    }
}
